package cn.edu.lcvc.ebuy_2023rj2.web.action.backstage.adminmanage;

import cn.edu.lcvc.ebuy_2023rj2.model.Admin;
import cn.edu.lcvc.ebuy_2023rj2.model.common.ResponseData;
import cn.edu.lcvc.ebuy_2023rj2.model.exception.MyServiceException;
import cn.edu.lcvc.ebuy_2023rj2.model.query.AdminQuery;
import cn.edu.lcvc.ebuy_2023rj2.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

//实现管理员模块的所有功能
@RestController
@RequestMapping(value = "/api/backstage/adminmanage")
public class AdminManageController {
    @Autowired
    AdminService adminService;

    //处理管理员列表请求
    @GetMapping("/list")
    public ResponseData list(AdminQuery adminQuery, Integer page, Integer pageSize, HttpServletRequest request) {

        return ResponseData.success(adminService.getAdmins(adminQuery, page, pageSize));
    }

    //删除指定id的记录,批量删除
    @DeleteMapping
    public ResponseData delete(@RequestBody Integer[] ids, HttpSession session, HttpServletRequest request) {//@PathVariable Integer id
        ResponseData responseData = null;
//        if (id != null) {
//            Admin admin = (Admin) session.getAttribute("admin");
//            if (admin.getId()!=id){
//                responseData = ResponseData.success(adminService.deleteAdmin(id));
//            }else {
//                responseData = ResponseData.error("删除失败，数据异常");
//            }
//
//        }

        if (ids != null) {
            Admin admin = (Admin) session.getAttribute("admin");
            try {
                responseData = ResponseData.success(adminService.deleteAdmins(ids, admin));
            } catch (MyServiceException e) {
                responseData = ResponseData.error(e.getMessage());
            }
        } else {
            responseData = ResponseData.error("删除失败，数据异常");
        }

        //执行删除操作后：需要重新读取数据库中现有的admin表中的所有记录
        return responseData;
    }

    @PostMapping
    public ResponseData doAdd(@RequestBody Admin admin) {
        ResponseData responseData = null;
        //2.保存进数据库
        //（1）先判断账户名是否重名
        if (admin.getUsername() == null || admin.getUsername().length() == 0) {//账户名不能为空
            responseData = ResponseData.error("添加失败，账户名不能为空");

        } else if (admin.getName() == null || admin.getName().length() == 0) {//姓名不能为空
            responseData = ResponseData.error("添加失败，姓名不能为空");

        } else if (admin.getSex() == null) {//姓名不能为空
            responseData = ResponseData.error("添加失败，性别不能为空");
        } else if (adminService.existsUsername(admin.getUsername())) {//如果重名
            responseData = ResponseData.error("添加失败，账户名重名，请选择其他的账户名！");

        } else {//如果不重名
            responseData = ResponseData.success(adminService.saveAdmin(admin));
        }
        //3.跳转回添加页面
        return responseData;
    }

    @GetMapping("/{id}")
    public ResponseData get(@PathVariable Integer id) {
        //根据id到数据库中查找对应的账户信息
        Admin admin = adminService.getAdminById(id);
        return ResponseData.success(admin);
    }

    @PutMapping
    public ResponseData doUpdate(@RequestBody Admin admin) {
        ResponseData responseData = null;
        //2.对数据库中相应的账户信息进行更新
        if (admin.getId() == null) {//id不能为空
            responseData = ResponseData.error("编辑失败:非法参数");
        } else if (admin.getUsername() == null || admin.getUsername().length() == 0) {//账户名不能为空
            responseData = ResponseData.error("编辑失败:账户名不能为空");

        } else if (admin.getName() == null || admin.getName().length() == 0) {//姓名不能为空
            responseData = ResponseData.error("编辑失败:姓名不能为空");

        } else if (adminService.existsByUsernameAndNotId(admin.getUsername(), admin.getId())) {//如果重名
            responseData = ResponseData.error("编辑失败:账户名重名，请选择其他的账户名！");

        } else {//如果不重名
            responseData = ResponseData.success(adminService.updateAdmin(admin));
        }
        return responseData;
    }
}